//
//  UTCounterViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/4.
//

import UIKit

class UTCounterViewController: UTBaseViewController {
    
    @IBOutlet weak var navigationHeight: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
    
    lazy var datas: Array<UTTaskCounterItemModel> = {
        return Array<UTTaskCounterItemModel>()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        loadDatas()
    }

    fileprivate func setupViews() {
        navigationHeight.constant = UIApplication.shared.statusBarFrame.size.height + 44
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none
        self.tableView.register(UINib(nibName: "UTFastCounterHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "UTFastCounterHeaderView")
        self.tableView.register(UINib(nibName: "UTTaskRecorderHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "UTTaskRecorderHeaderView")
        self.tableView.register(UINib(nibName: "UTTaskRecorderCell", bundle: nil), forCellReuseIdentifier: "UTTaskRecorderCell")
        self.tableView.estimatedRowHeight = UITableView.automaticDimension
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedSectionHeaderHeight = 361
        self.tableView.sectionHeaderHeight = UITableView.automaticDimension
        self.tableView.sectionFooterHeight = CGFloat.zero
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0.0
        }
    }
    
    
    fileprivate func loadDatas() {
        if let data = UTTaskCounterHelper.taskCounterInfo() {
            var list = Array<UTTaskCounterItemModel>()
            data.datas.forEach { item in
                if !item.isFinish {
                    list.append(item)
                }
            }
            
            datas = list.reversed()
            self.tableView.reloadData()
        }
    }
    
    @IBAction func goBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
}

extension UTCounterViewController: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 1 {
            return datas.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "UTFastCounterHeaderView") as? UTFastCounterHeaderView else {
                fatalError("Unable to dequeue headerView")
            }
            return headerView
        }
        guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "UTTaskRecorderHeaderView") as? UTTaskRecorderHeaderView else {
            fatalError("Unable to dequeue headerView")
        }
        headerView.reloadData = {
            self.loadDatas()
        }
        return headerView
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "UTTaskRecorderCell") as? UTTaskRecorderCell else {
            fatalError("Unable to dequeue cell")
        }
        cell.selectionStyle = .none
        cell.model = datas[indexPath.row]
        cell.reloadData = {
            self.loadDatas()
        }
        return cell
    }
    
}

